package 多线程.死锁;

/**
 * @author : K k
 * @date : 12:17 2020/10/11
 */
public class DeadLock {
    //两个资源
    public static volatile Object res1=new Object();;
    public static volatile Object res2=new Object();;


    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (DeadLock.res1){
                    //先获取res1资源
                    try {
                        Thread.sleep(1000);
                        synchronized (DeadLock.res2){
                            Thread.sleep(1000);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (DeadLock.res2){
                    //先获取res1资源
                    try {
                        Thread.sleep(1000);
                        synchronized (DeadLock.res1){
                            Thread.sleep(1000);
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();


    }
}
